<?php

namespace App\Http\Controllers;

use App\Models\BrotherCommunity;
use App\Models\HotTopic;
use App\Models\TranslateRecord;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Topic;
use App\Models\Link;
use App\Models\Banner;
use Phphub\Markdown\Markdown;

class CategoriesController extends Controller
{
    public function show($id, Request $request, Topic $topic)
    {
        // 获取分类及子分类
        $category     = Category::query()->with('subCategories')->findOrFail($id);
        // 获取子分类 ID 数组
        $categoriesId = array_column($category->relationsToArray()['sub_categories'], 'id');
        // 加入父分类 ID 数组，此时 $categoriesId 包含所有子分类 ID 和父分类 ID
        $categoriesId[] = $category->id;

        $topics      = $topic->getCategoriesTopicsWithFilter($request->get('filter', 'default'), $categoriesId);
        $links       = Link::allFromCache();
        $communities = BrotherCommunity::allFromCache();
        //$banners     = Banner::allByPosition();
        $hot_topics  = HotTopic::fetchCategory($id);

        if ($category->id == config('phphub.hunt_category_id')) {
            $topics->load('share_link');
            $view = 'share_links.index';
        }
        else
        {
            $view = 'topics.index';
        }

        $data = compact('topics', 'category', 'links', 'communities', 'banners', 'hot_topics');

        if ($category->id == config('phphub.translate_id'))
        {
            $translator_top3 = TranslateRecord::fetchTranslatorRank();
            $translators = TranslateRecord::fetchTranslatorsInMonth();
            $data = compact('topics', 'category', 'links', 'communities', 'banners', 'hot_topics','translators','translator_top3');
        }

        return view($view, $data);
    }
}
